﻿using AutoMapper;
using B.S.Basic.Domain;
using B.S.Basic.ErrorCode;
using B.S.Basic.Infratrucate;
using B.S.Basic.Read.API.Dtos;
using MediatR;

namespace B.S.Basic.Read.API.Application
{
    public class ShowCommandHandler : IRequestHandler<ShowCommand, APIResult<APIPaging<MyDTO>>>
    {
        private readonly IBaseRepositroy<MyExam> myRepository;
        private readonly IMapper mapper;

        public ShowCommandHandler(IBaseRepositroy<MyExam> myRepository, IMapper mapper)
        {
            this.myRepository = myRepository;
            this.mapper = mapper;
        }
        public Task<APIResult<APIPaging<MyDTO>>> Handle(ShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<MyDTO>> result = new APIResult<APIPaging<MyDTO>>();
            result.Code = EnumCode.查询成功;
            result.Message = EnumCode.查询成功.ToString();

            var list = from a in myRepository.GetAll()
                       select new MyDTO
                       {
                           MEmail = a.MEmail,
                           MId = a.MId,
                           MName = a.MName,
                           MNick = a.MNick,
                           MNum = a.MNum,
                           MNumber = a.MNumber,
                           MPhone = a.MPhone,
                           MTime = a.MTime,
                           Mwei = a.Mwei,
                           Mzhi = a.Mzhi,
                           Mzhu = a.Mzhu,
                           Mzu = a.Mzu,
                           RTime = a.RTime,
                           Sex = a.Sex,
                       };

            int recordCount = list.Count();
            int PageCount = (int)Math.Ceiling(recordCount * 1.0 / request.PageSize);
            list = list.Skip(request.PageSize * (request.PageIndex - 1)).Take(request.PageSize);

            APIPaging<MyDTO> paging = new APIPaging<MyDTO>();
            paging.RecordCount = recordCount;
            paging.PageCount=PageCount;
            paging.list=list.ToList();

            result.Data = paging;


            return Task.FromResult(result);
        }
    }
}
